﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AntiObesity.DatabaseAccess;

namespace AntiObesity.Base.Managers
{
    public partial class TargetHistoryManager
    {
        public static TargetHistory GetLatestHistory(Guid userID)
        {
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn, String.Format(
                    "SELECT * FROM [TargetHistories] WHERE [UserID]='{0}' AND [Date] IN " +
                    "(SELECT MAX(Date) FROM [TargetHistories] WHERE [UserID]='{0}')"
                    , userID));
                if (reader.Read())
                {
                    return CreateTargetHistory(reader);
                }
            }
            return null;
        }

        public static TargetHistory GetFirstHistory(Guid userID)
        {
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn, String.Format(
                    "SELECT * FROM [TargetHistories] WHERE [UserID]='{0}' AND [Date] IN " +
                    "(SELECT Min(Date) FROM [TargetHistories] WHERE [UserID]='{0}')"
                    , userID));
                if (reader.Read())
                {
                    return CreateTargetHistory(reader);
                }
            }
            return null;
        }

        public static void PersistHistory(Guid userID, float targetWeight)
        {
            var latestHistory = GetLatestHistory(userID);
            if (latestHistory == null || latestHistory.HasDifferentData(targetWeight))
            {
                Persist(new TargetHistory
                {
                    UserID = userID,
                    Date = DateTime.Now,
                    TargetWeight = targetWeight
                });
            }
            // Otherwise, do nothing
        }

        public static TargetHistory FindUserHistoryByUserAndDate(Guid userID, DateTime date)
        {
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn,
                    String.Format("SELECT * FROM [TargetHistories] WHERE [UserID]='{0}' AND [Date]='{1}'",
                    userID,
                    date));
                if (reader.Read())
                {
                    return CreateTargetHistory(reader);
                }
            }
            return null;
        }

        public static List<TargetHistory> GetAll(Guid userID)
        {
            var result = new List<TargetHistory>();
            using (var conn = DB.Instance.Current.GetConnection())
            {
                var reader = DB.Instance.Current.ExecuteForReader(conn,
                    String.Format("SELECT * FROM [TargetHistories] WHERE [UserID]='{0}' ORDER BY [Date] DESC ", userID));
                while (reader.Read())
                {
                    result.Add(CreateTargetHistory(reader));
                }
            }
            return result;
        }
    }
}
